package cn.xqj.product.service.impl;

import cn.xqj.basic.exception.BusinessException;
import cn.xqj.basic.util.BaiduAuditUtils;
import cn.xqj.basic.util.getLogininfo;
import cn.xqj.org.domain.Employee;
import cn.xqj.org.mapper.EmployeeMapper;
import cn.xqj.pet.domain.Pet;
import cn.xqj.pet.domain.PetDetail;
import cn.xqj.product.domain.Product;
import cn.xqj.product.domain.ProductDetail;
import cn.xqj.product.domain.ProductOnlineAuditLog;
import cn.xqj.product.domain.Product;
import cn.xqj.product.mapper.ProductDetailMapper;
import cn.xqj.product.mapper.ProductMapper;
import cn.xqj.product.mapper.ProductOnlineAuditLogMapper;
import cn.xqj.product.service.IProductService;
import cn.xqj.basic.service.impl.BaseServiceImpl;
import cn.xqj.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {


    @Autowired
    ProductMapper productMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    ProductDetailMapper productDetailMapper;


    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {

        Logininfo logininfo = getLogininfo.getLogininfo(request);
        Date onsaletime = new Date();

        for (Long id : ids) {
            Product product = productMapper.findOne(id);
            // Boolean textCensor = BaiduAuditUtils.TextCensor(product.getName());
            Boolean imgCensors = BaiduAuditUtils.ImgCensors(product.getResources());
            ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
            productOnlineAuditLog.setProductId(product.getId());
            Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
            productOnlineAuditLog.setAuditId(employee.getId());
            productOnlineAuditLog.setAuditTime(new Date());
            if (true && imgCensors) {
                product.setState(0l);
                product.setOnsaletime(onsaletime);
                productMapper.update(product);
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setNote("审核成功");
            } else {
                productOnlineAuditLog.setState(-1);
                productOnlineAuditLog.setNote("审核失败");
                throw new BusinessException("审核失败,含有非法字符或图片！");
            }
            productOnlineAuditLogMapper.add(productOnlineAuditLog);
        }


    }

    @Override
    public void offsale(Long[] ids) {
        Date time = new Date();
        productMapper.offsale(ids, time);
    }

    @Transactional
    @Override
    public void add(Product product) {
        super.add(product);

        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.updateProductDetailByProductId(productDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        productDetailMapper.delProductDetailByProductId(id);
    }

}
